#===============================================================================
.SUFFIXES:

#===============================================================================
ifndef SOURCE_ROOT
  SOURCE_ROOT=.
endif

ifndef OUT_ROOT
  OUT_ROOT=./out
endif

ifndef CROSS_COMPILE
#  CROSS_COMPILE=arm-none-eabi-
endif

#===============================================================================
AS      = $(CROSS_COMPILE)as
LD      = $(CROSS_COMPILE)ld
CC      = $(CROSS_COMPILE)gcc
CPP     = $(CROSS_COMPILE)cpp
AR      = $(CROSS_COMPILE)ar
NM      = $(CROSS_COMPILE)nm
STRIP   = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
RANLIB  = $(CROSS_COMPILE)ranlib

MKDIR = mkdir
COPY = cp

#Q       = @echo
Q       = @
#Q       = 

#===============================================================================
DIRS =

TARGET = aliyun_test

# core
INCLUDES = \
	-I/usr/include/libxml2 \
	-I../prebuild/gtest-1.7.0/include \
	-I../inc
DEFINES = 
SOURCES = main.cpp \
	core/test_acs_request.cpp \
	core/test_acs_response.cpp \
	core/test_default_acs_client.cpp \
	core/auth/test_shahmac1_signer.cpp \
	core/auth/test_shahmac256_signer.cpp \
	core/auth/test_credential.cpp \
	core/http/test_http_response.cpp \
	core/http/test_http_helper.cpp \
	core/reader/test_json_reader.cpp \
	core/regions/test_product_domain.cpp \
	core/regions/test_end_point.cpp \
	core/regions/test_end_point_provider.cpp \
	core/utils/test_string_helper.cpp \


# ecs
INCLUDES += -I./
SOURCES += \
	Ecs/2014-05-26/test_ecs_demo.cpp \


LIBS = -lstdc++ -lcurl -lxml2 -L../out -laliyun -L../prebuild/gtest-1.7.0/gcc4.5.2_x64 -lgtest -lpthread
CCFLAGS =  -Wall $(INCLUDES) $(DEFINES)

#===============================================================================

OBJECTS = $(patsubst %.cpp,$(OUT_ROOT)/%.o, $(SOURCES))
OBJECTS_DIRS = $(foreach f, $(OBJECTS), $(dir $(f)))

DIRS += $(OUT_ROOT)
DIRS := $(sort $(DIRS))

TARGET_EXE  = $(OUT_ROOT)/$(TARGET)

#===============================================================================
.PHONY: default target clean

vpath %.c ${SOURCE_ROOT}

default: $(DIRS) target

target : $(TARGET_EXE)

$(DIRS):
	$(Q) -$(MKDIR) -p $(DIRS)

$(OUT_ROOT)/%.d: %.cpp
	$(shell test -d $(@D) || mkdir -p $(@D))
	$(Q) set -e; $(CC) -MM $(CCFLAGS) $< > $@.tmp; \
	sed 's,\($(*F)\)\.o[ :]*,\$@ : Makefile ,g' < $@.tmp > $@; \
	sed 's,\($(*F)\)\.o[ :]*,\$(OUT_ROOT)/$*.o : Makefile ,g' < $@.tmp >> $@; \
	echo "\t$(Q) $(CC) $(CCFLAGS) -c -x c++ -o $(OUT_ROOT)/$*.o $<" >> $@; \
	rm -f $@.tmp

ifneq ($(MAKECMDGOALS),clean)
sinclude $(patsubst %.cpp,$(OUT_ROOT)/%.d, $(SOURCES))
endif

$(TARGET_EXE) : $(OBJECTS)
	$(Q) $(CC) -o $@ $^ $(LIBS)

$(TARGET_SO) : $(OBJECTS)
	$(Q) $(CC) -shared -fPIC $(LIBS) -o $@ $^

clean:
	rm -r -f $(OUT_ROOT)

